﻿Imports System.Management.Automation
Imports System.Management.Instrumentation
Imports System.Management.Automation.Runspaces

Module modPowerShell
    Private host As New PowerShellWorkBenchHost
    Private r As Runspace = RunspaceFactory.CreateRunspace(host)
    Public Sub CreateVariable(ByVal strVarName As String, ByVal pso As PSObject)
        r.SessionStateProxy.SetVariable(strVarName, pso)
    End Sub
    Public Sub InvokeString(ByVal strScript As String)
        Dim ps As PowerShell = PowerShell.Create()
        ps.Runspace = r
        ps.AddScript(strScript)
        ps.AddCommand("out-default")
        ps.Commands.Commands.Item(ps.Commands.Commands.Count - 1).MergeUnclaimedPreviousCommandResults = PipelineResultTypes.Error Or PipelineResultTypes.Output
        Dim output As System.Collections.ObjectModel.Collection(Of PSObject)
        output = ps.Invoke()

    End Sub
    Public Sub InitializeRunspace()
        r.Open()
        InvokeString("remove-item function:clear-host")
        r.RunspaceConfiguration.Cmdlets.Prepend(New CmdletConfigurationEntry("clear-host", GetType(ClearHost), Nothing))
        r.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("new-tree", GetType(NewTree), Nothing))
        r.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("new-treenode", GetType(NewTreeNode), Nothing))
        r.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("clear-tree", GetType(ClearTree), Nothing))
        r.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("remove-tree", GetType(RemoveTree), Nothing))
        r.RunspaceConfiguration.Cmdlets.Append(New CmdletConfigurationEntry("new-typemenuitem", GetType(NewTypeMenuItem), Nothing))
        r.RunspaceConfiguration.Cmdlets.Update()

    End Sub
End Module
